package com.jweb.framework.core;

import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.DefaultMessageSourceResolvable;


public class RuntimeSuperException extends RuntimeException implements MessageSourceResolvable {
	
	private static final long serialVersionUID = -3617744653279475003L;
	private final DefaultMessageSourceResolvable resolvable ;
	private boolean alreadyLogged ;
	
	public boolean isAlreadLogged(){
		return this.alreadyLogged ;
	}
	
	public StackTraceElement[] getStackTrace(){
		this.alreadyLogged = true ;
		StackTraceElement[] st = super.getStackTrace() ;
		return st ;
	}
	
	public RuntimeSuperException(String code) {
		super(getMessage(code , null));
		this.resolvable = new DefaultMessageSourceResolvable(code);
	}
	
	public RuntimeSuperException(String code , Object[] arguments){
		super(getMessage(code , null));
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments) ;
	}
	
	public RuntimeSuperException(String code , Throwable throwable) {
		super(getMessage(code , null) , throwable) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , null , throwable.getMessage()) ;
	}
	
	public RuntimeSuperException(String code , Object[] arguments , Throwable throwable){
		super(getMessage(code , null) , throwable) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments , throwable.getMessage());
	}
	
	public RuntimeSuperException(String code , String defaultMessage){
		super(getMessage(code , defaultMessage));
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , null , defaultMessage);
	}
	
	public RuntimeSuperException(String code , Object[] arguments , String defaultMessage){
		super(getMessage(code , defaultMessage));
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments , defaultMessage);
	}
	
	public RuntimeSuperException(String code , String defaultMessage , Throwable throwable){
		super(getMessage(code , defaultMessage) , throwable) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , new Object[]{throwable.getClass().getName()} , defaultMessage);
	}
	
	public RuntimeSuperException(String code , Object[] argumnets , String defaultMessage, Throwable throwable){
		super(getMessage(code , defaultMessage) , throwable) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , argumnets , defaultMessage) ;
	}
	
	private static String getMessage(String code , String defaultMessage) {
		if(code == null){
			return defaultMessage ;
		}
		if(defaultMessage == null){
			return "[" + code + "]" ;
		}
		
		return "[" + code + "]" + defaultMessage ;
	}
	
	public Object[] getArguments() {
		return this.resolvable.getArguments() ;
	}
	
	public String[] getCodes() {
		return this.resolvable.getCodes() ;
	}
	
	public String getDefaultMessage() {
		return this.resolvable.getDefaultMessage() ;
	}
}
